package com.palm.vue.integration;

import com.palm.vue.VueResourceLoader;
import com.palm.vue.VueResources;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Handler;

public class VueHandler implements Handler {
    @Override
    public void handle(Context ctx) throws Throwable {
        String path = ctx.path();
        VueResources data = VueResourceLoader.get(path);
        if (data == null||!data.isExists()) {
            ctx.status(404);
        } else {
            ctx.setHandled(true);
            String modified_since = ctx.header("If-Modified-Since");
            String modified_now = XPluginImp.modified_time.toString();
            if (modified_since != null && XPluginImp.maxAge() > 0 && modified_since.equals(modified_now)) {
                ctx.headerSet("Cache-Control", "max-age=" + XPluginImp.maxAge());
                ctx.headerSet("Last-Modified", modified_now);
                ctx.status(304);
            } else {

                if (XPluginImp.maxAge() > 0) {
                    ctx.headerSet("Cache-Control", "max-age=" + XPluginImp.maxAge());
                    ctx.headerSet("Last-Modified", XPluginImp.modified_time.toString());
                }
                if(data.isJs()){
                    ctx.contentType("text/javascript");
                    ctx.headerSet("Content-Encoding","gzip");
                }else{
                    ctx.contentType("text/html");
                    ctx.headerSet("Content-Encoding","gzip");
                }
                ctx.status(200);
                ctx.output(data.getData());
            }
        }
    }
}
